package com.semlab.client.mvp.home.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.semlab.client.proxy.ClientProxy;
import com.semlab.client.proxy.MoviePageProxy;
import com.semlab.client.proxy.MovieProxy;
import com.semlab.client.widgets.ListPager;
import com.semlab.client.widgets.UICallback;

import java.util.List;

public abstract class MoviesExplorer extends Composite {

    private static MoviesExplorerUiBinder uiBinder = GWT.create(MoviesExplorerUiBinder.class);

    interface MoviesExplorerUiBinder extends UiBinder<Widget, MoviesExplorer> {
    }

    MoviePageProxy data;
    UICallback<MovieProxy> detailsCallback;
    UICallback<List<ClientProxy>> friendsCallback;
    UICallback<MoviePageProxy> moviesCallback;

    MovieCell selected;

    @UiField
    FlowPanel moviesList;
    @UiField
    MovieDetails detailsPanel;
    @UiField
    ListPager listPager;
    @UiField
    ScrollPanel leftScroll;

    public MoviesExplorer() {
        detailsCallback = new UICallback<MovieProxy>() {
            @Override
            public void callback(MovieProxy object) {
                detailsPanel.getElement().getStyle().setDisplay(Display.BLOCK);
//                detailsPanel.setWidget(new MovieDetails(object));
                detailsPanel.setMovie(object);
            }
        };
        friendsCallback = new UICallback<List<ClientProxy>>() {
            @Override
            public void callback(List<ClientProxy> object) {

            }
        };

        moviesCallback = new UICallback<MoviePageProxy>() {
            @Override
            public void callback(MoviePageProxy object) {
                setData(object);
            }
        };

        initWidget(uiBinder.createAndBindUi(this));
        leftScroll.getElement().getParentElement().getStyle().setHeight(100, Unit.PCT);
        leftScroll.getElement().getFirstChildElement().getStyle().setHeight(100, Unit.PCT);
        detailsPanel.getElement().getParentElement().getStyle().setHeight(100, Unit.PCT);
    }

    @UiFactory
    ListPager makePager() {
        return new ListPager(15) {
            @Override
            public void showSubset(int offset, int num) {
                getMovies(moviesCallback, offset, num);
            }
        };
    }

    public MoviePageProxy getData() {
        return data;
    }

    public void setData(MoviePageProxy data) {

        GWT.log("setting data=" + data);
        this.data = data;
        moviesList.clear();
        for (MovieProxy movieProxy : data.getItems()) {
            final MovieCell cell = new MovieCell(movieProxy) {
                @Override
                public void enrichMovieData(MovieProxy movie) {
                    MoviesExplorer.this.enrichMovieData(movie, detailsCallback);
                }

                @Override
                public void showDetails(MovieProxy movie) {
//                    GWT.log("show movie details: " + movie.getTitle());
                    getDetailsForAMovie(movie, detailsCallback);
                    getFriendsForAMovie(movie, friendsCallback);
                }
            };
            cell.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    if(selected != null){
                        selected.setSelected(false);
                    }
                    selected = cell;
                    cell.setSelected(true);
                }
            });
            moviesList.add(cell);
        }
        getDetailsForAMovie(data.getItems().get(0), detailsCallback);
        MovieCell first = (MovieCell) moviesList.getWidget(0);
        first.setSelected(true);
        selected = first;
        listPager.setTotal(data.getTotal());
    }

    public void refresh() {
        listPager.goToPage(0);
    }

    public abstract void enrichMovieData(MovieProxy movie, UICallback<MovieProxy> detailsCallback);

    public abstract void getDetailsForAMovie(MovieProxy proxy, UICallback<MovieProxy> callback);

    public abstract void getFriendsForAMovie(MovieProxy proxy, UICallback<List<ClientProxy>> friendsCallback);

    public abstract void getMovies(UICallback<MoviePageProxy> callback, int offset, int num);

}
